package com.linzhaowei.headhuntingservice.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hyphenate.chat.EMClient;
import com.linzhaowei.headhuntingservice.InterviewerUpdatePersonal;
import com.linzhaowei.headhuntingservice.JobInformationManage;
import com.linzhaowei.headhuntingservice.R;
import com.linzhaowei.headhuntingservice.Receive;
import com.linzhaowei.headhuntingservice.Start;
import com.linzhaowei.headhuntingservice.bean.User;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;
import java.util.Objects;

public class InterviewerPersonalFragment extends Fragment implements View.OnClickListener {
    TextView name;
    Button updatePersonal;
    Button jobInformation;
    Button exit;
    String setName;
    final static int SELECT_INTERVIEWER = 1;
    View view;
    Button receive;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_interviewer_personal, container, false);
        initViews();
        setEvents();

        selectInterviewer();

        return view;
    }

    void initViews() {
        name = view.findViewById(R.id.i_p_name);
        updatePersonal = view.findViewById(R.id.i_p_update_personal);
        jobInformation = view.findViewById(R.id.i_p_job_information);
        receive=view.findViewById(R.id.i_p_receive);
        exit=view.findViewById(R.id.i_p_exit);
    }

    void setEvents() {
        updatePersonal.setOnClickListener(this);
        jobInformation.setOnClickListener(this);
        receive.setOnClickListener(this);
        exit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.i_p_update_personal:
                Intent intent = new Intent(getActivity(), InterviewerUpdatePersonal.class);
                startActivity(intent);
                break;
            case R.id.i_p_job_information:
                Intent intent2 = new Intent(getActivity(), JobInformationManage.class);
                startActivity(intent2);
                break;
            case R.id.i_p_receive:
                Intent intent3=new Intent(getActivity(), Receive.class);
                startActivity(intent3);
                break;
            case R.id.i_p_exit:
                Intent intent4=new Intent(getActivity(), Start.class);
                startActivity(intent4);
                EMClient.getInstance().logout(true);
                Objects.requireNonNull(getActivity()).finish();
                break;
            default:
                break;
        }
    }

    void selectInterviewer() {
        User user = new User();
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("user", Context.MODE_PRIVATE);
        user.setId(sharedPreferences.getInt("id", -1));
        new Thread() {
            @Override
            public void run() {
                HttpUtils httpUtils = new HttpUtils();
                try {
                    String result = httpUtils.get(Ip.ip + "/user/select_interviewer?id=" + String.valueOf(user.getId()));
                    JSONArray jsonArray=JSON.parseArray(result);
                    JSONObject jsonObject = jsonArray.getJSONObject(0);
                    setName = jsonObject.getString("name");
                    Message message = handler.obtainMessage();
                    message.what = SELECT_INTERVIEWER;
                    handler.sendMessage(message);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SELECT_INTERVIEWER:
                    name.setText(setName);
                    break;
                default:
                    break;
            }
        }
    };


}
